package com.cloud.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.dao.PaymentMapper;
import com.cloud.service.IPaymentService;
import com.cloud.entity.Payment;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <h3>springcloud02</h3>
 * <p></p>
 *
 * @author : zhengyue
 * @date : 2020-06-02 09:00
 **/
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment>
        implements IPaymentService {

    /**
     * 正确访问
     * @param id
     * @return
     */
    public String paymentInfo_OK(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "paymentInfo_OK, id：" + id + "O(∩_∩)O";
    }

    /**
     * 超时访问
     * @param id
     * @return
     */
    // 3s以内走正常的业务逻辑，3s外降级
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String paymentInfo_Timeout(Integer id) {
        int timeNumber = 5;
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池：" + Thread.currentThread().getName() + "paymentInfo_Timeout, id："
                + id + ",O(∩_∩)O 耗时："+ timeNumber + "s";
    }

    public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "paymentInfo_Timeout, id："
                + id + ",耗时："+ "系统繁忙，请稍后再试" + "/(ㄒoㄒ)/~~";
    }
}
